package www.dashu.com.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import www.dashu.com.api.CommonResult;
import www.dashu.com.domain.OmsCartItem;
import www.dashu.com.dto.OmsCartItemQuery;
import www.dashu.com.service.OmsCartItemService;
import www.dashu.com.service.RedisService;

import java.util.Date;

@Controller
@Api(tags = "OmsCartItemController", description = "购物车表")
@RequestMapping("/cartItem")
public class OmsCartItemController
{

    @Autowired
    OmsCartItemService cartItemService;
    @Autowired
    RedisService redisService;

    @ApiOperation(value = "购物车表列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Page<OmsCartItem>> list(OmsCartItemQuery query) {

        try {
            Page<OmsCartItem> pageData = cartItemService.findAll(query);
            return CommonResult.success(pageData);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @ApiOperation(value = "新增购物车表")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody OmsCartItem cartItem) {
        cartItem.setId(cartItemService.createId(cartItem));

        cartItem.setCreateTime(new Date());
        cartItem.setUpdateTime(new Date());

        cartItemService.save(cartItem);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改购物车表")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody OmsCartItem cartItem) {

        cartItem.setUpdateTime(new Date());

        cartItemService.save(cartItem);
        return CommonResult.success();
    }

    @ApiOperation(value = "删除购物车表")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public CommonResult delete(@RequestBody OmsCartItem cartItem) {
        cartItemService.deleteById(cartItem.getId());
        return CommonResult.success();
    }

}
